window.onload = function () {
    if (!document.getElementsByClassName) {
        document.getElementsByClassName = function (cls) {
            var ret = [];
            var els = document.getElementsByTagName('*');
            for (var i = 0, len = els.length; i < len; i++) {
                if (els[i].className === cls
                    || els[i].className.indexOf(cls + ' ') >= 0
                    || els[i].className.indexOf(' ' + cls + ' ') >= 0
                    || els[i].className.indexOf(' ' + cls) >= 0) {
                    ret.push(els[i]);
                }
            }
            return ret;
        }
    }

    var cartTable = document.getElementById('cartTable');
    var tr = cartTable.getElementsByTagName("ul");
    var checkInputs = document.getElementsByClassName('check');
    var checkAllInputs = document.getElementsByClassName('check-all');
    var selectedTotal = document.getElementById('J_SelectedItemsCount');
    var priceTotal = document.getElementById('J_Total');
    var commitShoppingCar=document.getElementById('J_Go');
    commitShoppingCar.style.backgroundColor = "#FF5000";

    //计算
    function getTotal() {
        var seleted = 0;
        var price = 0;
        var HTMLstr = '';
        for (var i = 0, len = tr.length; i < len; i++) {
            if (tr[i].getElementsByTagName('input')[0].checked) {
                seleted += parseInt(tr[i].getElementsByTagName('input')[2].value);		
                price += parseFloat(tr[i].getElementsByTagName("li")[5].innerHTML);
            }
        }

        selectedTotal.innerHTML = seleted;
        priceTotal.innerHTML = price.toFixed(2);
    }

    //小计
    function getSubTotal(tr) {
        var tds = tr.getElementsByTagName("li");
        var price = parseFloat(tds[3].children[0].innerHTML);
        var count = parseInt(tr.getElementsByTagName('input')[2].value);
        var SubTotal = parseFloat(price * count);
        tds[5].innerHTML = SubTotal.toFixed(2);
    }
	
    
    
    for (var i = 0 , len = checkInputs.length; i < len; i++) {
        checkInputs[i].onclick = function () {
            if (this.className === 'check-all check') {
                for (var j = 0; j < checkInputs.length; j++) {
                    checkInputs[j].checked = this.checked;
                }
            }
            if (this.checked == false) {
                for (var k = 0; k < checkAllInputs.length; k++) {
                    checkAllInputs[k].checked = false;
                }
            }
            getTotal();
        }
    }
    

    for (var i = 0; i < tr.length; i++) {
    	getSubTotal(tr[i]);
    	getTotal();
        tr[i].onclick = function (e) {
            e = e || window.event;
            var el = e.srcElement;
            var cls = el.className;
            var input = this.getElementsByTagName('input')[2];
            var val = parseInt(input.value);
            var reduce = this.getElementsByTagName('input')[1];
            switch (cls) {
                case 'add am-btn':
                    input.value = val + 1;
                    reduce.innerHTML = '-';
                    getSubTotal(this);
                    
                    var id=this.getElementsByTagName('input')[0];
                    var count=parseInt(this.getElementsByTagName('input')[2].value);
                    window.location.href="updateShoppingCarCountAction.action?updateShoppingCarId="+id.value+"&updateShoppingCarCount="+count;
                    
                    break;
                case 'min am-btn':
                    if (val > 1) {
                        input.value = val - 1;
                    }
                    if (input.value <= 1) {
                        reduce.innerHTML = '';
                    }
                    getSubTotal(this);
                    
                    var id=this.getElementsByTagName('input')[0];
                    var count=parseInt(this.getElementsByTagName('input')[2].value);
                    window.location.href="updateShoppingCarCountAction.action?updateShoppingCarId="+id.value+"&updateShoppingCarCount="+count;
                    
                    break;
                case 'delete':
                    var conf = confirm('确定要删除吗？');
                    if (conf) {
                    	var id=this.getElementsByTagName('input')[0];
                    	this.parentNode.removeChild(this);
                        location.href="deleteShoppingCarAction.action?deleteShoppingCarId="+id.value;
                    }
                    break;
                default :
                    break;
            }
            getTotal();
        }
        tr[i].getElementsByTagName('input')[2].onkeyup = function () {
            var val = parseInt(this.value);
            var tr = this.parentNode.parentNode.parentNode.parentNode.parentNode
            if (isNaN(val) || val < 1) {
                val = 1;
            }
            else if(val > 99){
            	val = 99;
            }
            this.value = val;
            getSubTotal(tr);
            getTotal();
            
            var id=tr.getElementsByTagName('input')[0];
            window.location.href="updateShoppingCarCountAction.action?updateShoppingCarId="+id.value+"&updateShoppingCarCount="+val;
            
        }
    }
    

    checkAllInputs[0].checked = true;
    checkAllInputs[0].onclick();
    commitShoppingCar.onclick = function () {

    	var seleteShoppingCar=new Array();
    	for (var i = 0; i < tr.length; i++) {
            var input = tr[i].getElementsByTagName('input')[0];
            var id=parseInt(input.value);
            if (input.checked) {
            	seleteShoppingCar.push(id);
            }
        }
    	
        window.location.href="selectShoppingCarToOrderAction.action?seleteShoppingCar="+seleteShoppingCar;

    }
}

